package cn.kgc.controller;

import cn.kgc.base.dto.UmsRoleDTO;
import cn.kgc.client.UmsRoleFeignClient;
import cn.kgc.commons.annotation.AutoDict;
import cn.kgc.commons.exception.HttpException;
import cn.kgc.commons.util.UserContextHolder;
import cn.kgc.commons.vo.Result;
import cn.kgc.dto.add.WmsPurchaseAddDTO;
import cn.kgc.entity.WmsPurchase;
import cn.kgc.service.WmsPurchaseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @description: 采购单控制器
 * @author: 石浩杰
 * @date: 2022-12-07 11:32
 */
@RestController
@Api(tags = "采购单控制器")
@RequestMapping("/wms/purchase")
@Validated
@Slf4j
public class WmsPurchaseController {

    @Resource
    private WmsPurchaseService wmsPurchaseService;

    @Resource
    private UmsRoleFeignClient umsRoleFeignClient;

    @PostMapping("/accept")
    @CrossOrigin
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "query")
    @ApiOperation(value = "接受采购单", produces = "application/json;charset=utf-8",response = Result.class,httpMethod = "POST")
    public boolean acceptWmsPurchase(@NotNull(message = "采购单id不为空") @RequestParam Long id){
        return wmsPurchaseService.acceptWmsPurchase(id);
    }

    @PostMapping("/save")
    @CrossOrigin
    @ApiOperation(value = "新建采购单", produces = "application/json;charset=utf-8",response = Result.class,httpMethod = "POST")
    public boolean saveWmsPurchase(@RequestBody WmsPurchaseAddDTO wmsPurchaseAddDTO){

        return wmsPurchaseService.saveWmsPurchase(wmsPurchaseAddDTO);
    }
    //查看采购单信息，普通采购人员可以查看自己的采购单，采购的管理查询人员，查询所有
    @GetMapping("/list")
    @CrossOrigin
    @ApiOperation(value = "查看采购单信息",httpMethod = "GET",produces = "application/json;charset=utf-8",response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wmsPurchaseId", value = "采购单编号",dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "currentPage", value = "当前页",dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", dataType = "int", paramType = "query")
    })
    @AutoDict
    public IPage<WmsPurchase> selectAll(@RequestParam(defaultValue = "0") long wmsPurchaseId,@RequestParam(defaultValue = "1") int currentPage,@RequestParam(defaultValue = "5") int pageSize){
        //long memberId= Long.parseLong(UserContextHolder.getInstance().getUserId());
        long memberId=1L;
        //根据用户的id获取用户的角色
        List<UmsRoleDTO> list=umsRoleFeignClient.getUmsRole(memberId).getData();
        if (list==null){
            throw new HttpException(40002,500);
        }

        boolean flag=false; //是否是采购管理员
        for (UmsRoleDTO umsRoleDTO : list) {
            if (umsRoleDTO.getRoleCode().equals("CGGLY")){
                flag=true;
                break;
            }
        }
        //如果是采购管理员，能够查询所有的采购单信息，如果不是采购管理员只能查看自己的
        LambdaQueryWrapper<WmsPurchase> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if (!flag){
            lambdaQueryWrapper.eq(WmsPurchase::getAssigneeId,memberId);
        }
        //按照采购单的id进行查询
        lambdaQueryWrapper.eq(wmsPurchaseId!=0,WmsPurchase::getId,wmsPurchaseId);
        IPage<WmsPurchase> page=new Page<>(currentPage,pageSize);
        return wmsPurchaseService.page(page,lambdaQueryWrapper);
    }
    //采购人员接收采购任务

    //采购管理员将采购单分配给采购人员
    @PostMapping("/allocation")
    @CrossOrigin
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "采购单id", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "assigneeId", value = "采购员id", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "assigneeName", value = "采购员姓名", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "采购员电话", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "分配采购单", produces = "application/json;charset=utf-8",response = Result.class,httpMethod = "POST")
    public boolean allocationWmsPurchase(@RequestParam Long id, @NotNull(message = "采购员id不为空") @RequestParam Long assigneeId, @NotBlank(message = "采购员id不为空") @RequestParam String assigneeName,@NotBlank(message = "采购员id不为空") @RequestParam String phone){
        return wmsPurchaseService.allocationWmsPurchase(id,assigneeId,assigneeName,phone);
    }



    @PostMapping("/finish")
    @CrossOrigin
    @ApiImplicitParam(name = "id", value = "采购单id", required = true, dataType = "Long", paramType = "query")
    @ApiOperation(value = "完成采购单", produces = "application/json;charset=utf-8",response = Result.class,httpMethod = "POST")
    public boolean finishWmsPurchase(@NotNull(message = "采购单id不为空") @RequestParam Long id){
        return wmsPurchaseService.finishWmsPurchase(id);
    }
}
